/**
 * 
 */
package inet.app.model.util;

import android.app.ProgressDialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;

/**
 * @author blue dragon
 *
 */
public class ControllableProgressDialog {
	private Context context;
	private ProgressDialog pDialog;
	
	private String title;
	private String message;
	private TimeConsumingTaskRunnable task;
	private Thread thread;
	
	/**
	 * 
	 * @param context
	 * @param tcTask: your implementation of time consuming Task
	 * @param timeOut: after time out, thread will be stop to avoid loop forever
	 * @param title: title appear in Progress Dialog
	 * @param message: message appear in Progress Dialog
	 */
	public ControllableProgressDialog(Context context, TimeConsummingTaskInterface tcTask, String title, String message){
		this.context = context;		
		this.task = new TimeConsumingTaskRunnable(tcTask);		
		this.title = title;
		this.message = message;
	}
	
	public void run(){
		
		pDialog = 
			ProgressDialog.show(context, title,message, true,true, stopListener);
		//pDialog.setButton("Dừng", stopListener);
		task.setProcessDialog(pDialog);
		thread = new Thread(task);
		thread.start();
	}
	
	OnCancelListener stopListener = new OnCancelListener() {
		@Override
		public void onCancel(DialogInterface arg0) {
			// TODO Auto-generated method stub
			task.stop();
		}

		
		
		
	};

	
}
